package com.senven.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.senven.api.commom.ResponsePage;
import com.senven.api.commom.ResponseResult;
import com.senven.api.dao.DepartmentMapper;
import com.senven.api.pojo.Department;
import com.senven.api.service.DepartmentService;
import com.senven.api.utils.ReponseResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LQP
 * 代码不足：应该将Page封装成一个高级类
 */
@RequestMapping("/api/organ/department")
@Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 分页查询社团所有部门列表
     * @param
     * @return
     */
    @GetMapping("/getDepartmentList")
    @ResponseBody
    public ResponsePage<Department> queryDepartmentList(@RequestParam(value = "current",defaultValue = "1") int current,
                                                @RequestParam(value = "size",defaultValue = "10") int size,
                                                @RequestParam("oid")int oid){
        return departmentService.getDepartmentList(oid, current, size);
    }

    /**
     * 根据部门ID查询部门, 单个不分页
     * @param oid,did
     * @return
     */
    @GetMapping("/getDepartmentById")
    @ResponseBody
    public Department queryDepartmentById(@RequestParam("oid") int oid,
                                          @RequestParam("did") int did){
        return departmentService.queryDepartmentById(did,oid);
    }

    /**
     * 分页查询，根据部门名称查询
     * @param oid
     * @param name
     * @param current
     * @param size
     * @return
     */
    @GetMapping("/getDepartmentByName")
    @ResponseBody
    public ResponsePage<Department> queryDepartmentByName(@RequestParam("oid")int oid,
                                                          @RequestParam("name")String name,
                                                          @RequestParam(value = "current", defaultValue = "1")int current,
                                                          @RequestParam(value = "size", defaultValue = "10")int size){
        return departmentService.queryDepartmentByName(oid,name,current,size);
    }


    @GetMapping("/deleteDepartmentById")
    @ResponseBody
    public ResponseResult deleteDepartment(@RequestParam("oid")int oid,
                                           @RequestParam("name")int did){
        int result = departmentService.deleteDepartment(oid,did);
        if (result == 0){
            return ReponseResultUtil.error(-1,"部门删除失败");
        }else{
            return ReponseResultUtil.success("部门删除成功");
        }
    }

    @RequestMapping("/updateDepartment")
    @ResponseBody
    public ResponseResult updateDepartment(@RequestBody Department department){
        int result = departmentService.updateDepartment(department);
        if (result == 0){
            return ReponseResultUtil.error(-1,"部门更新失败");
        }else{
            return ReponseResultUtil.success("部门更新成功");
        }
    }

    @RequestMapping("/insertDepartment")
    @ResponseBody
    public ResponseResult insertDepartment(@RequestBody Department department){
        int result = departmentService.insertDepartment(department);
        if (result == 0){
            return ReponseResultUtil.error(-1,"部门插入失败");
        }else{
            return ReponseResultUtil.success("部门插入成功");
        }
    }

}
